package com.example.everything_is_possible;

import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class translateActivity extends AppCompatActivity {

    private Button translate;

    private Button mbtn_back;
    private Spinner spinner1, spinner2;
    private EditText text1;
    private TextView text2;
    private NetworkChangeReceiver networkChangeReceiver;
    private IntentFilter intentFilter;
    private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_translate);
        initView();

        mbtn_back = findViewById(R.id.btn_back8);

        mbtn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    private void initView() {
        spinner1 = findViewById(R.id.spinner1);//源语言
        spinner2 = findViewById(R.id.spinner2);//目的语言
        text1 = findViewById(R.id.text1);//输入框
        text2 = findViewById(R.id.text2);//显示框

        translate = findViewById(R.id.translate);
        intentFilter = new IntentFilter();
        intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        networkChangeReceiver = new NetworkChangeReceiver();
        registerReceiver(networkChangeReceiver, intentFilter);


        translate.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {
                String Content1 = spinner1.getSelectedItem().toString();
                String Content2 = spinner2.getSelectedItem().toString();
                final String text123 = text1.getText().toString();
                if (TextUtils.isEmpty(text123)) {
                    Toast.makeText(com.example.everything_is_possible.translateActivity.this, "请输入内容", Toast.LENGTH_SHORT).show();
                    return;
                }
                Toast ts = Toast.makeText(getBaseContext(),"请选择不同语言",Toast.LENGTH_LONG);
                /*

                联网提示

                 */

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String from;
                        String to;
                        //根据spinner选中值进行if选择翻译
                        if (Content1.equals("智能检测")  && Content2.equals("英语")){
                            from = "auto";
                            to = "en";
                        } else if(Content1.equals("智能检测")  && Content2.equals("中文")){
                            from = "auto";
                            to = "zh";
                        } else if(Content1.equals("智能检测")  && Content2.equals("法语")){
                            from = "auto";
                            to = "fra";
                        }else if(Content1.equals("智能检测")  && Content2.equals("日文")){
                            from = "auto";
                            to = "jp";
                        }else if(Content1.equals("智能检测")  && Content2.equals("西班牙语")){
                            from = "auto";
                            to = "spa";
                        }else if(Content1.equals("智能检测")  && Content2.equals("俄语")){
                            from = "auto";
                            to = "ru";
                        }else if(Content1.equals("智能检测")  && Content2.equals("德语")){
                            from = "auto";
                            to = "de";
                        }else if(Content1.equals("英语") && Content2.equals("中文")){
                            from = "en";
                            to = "zh";
                        }else if(Content1.equals("英语") && Content2.equals("法语")){
                            from = "en";
                            to = "fra";
                        }else if(Content1.equals("英语") && Content2.equals("日文")){
                            from = "en";
                            to = "jp";
                        }else if(Content1.equals("英语") && Content2.equals("西班牙语")){
                            from = "en";
                            to = "spa";
                        }else if(Content1.equals("英语") && Content2.equals("俄语")){
                            from = "en";
                            to = "ru";
                        }else if(Content1.equals("英语") && Content2.equals("德语")){
                            from = "en";
                            to = "de";
                        }else if(Content1.equals("中文") && Content2.equals("英语")){
                            from = "zh";
                            to = "en";
                        }else if(Content1.equals("中文") && Content2.equals("法语")){
                            from = "zh";
                            to = "fra";
                        }else if(Content1.equals("中文") && Content2.equals("日语")){
                            from = "zh";
                            to = "jp";
                        }else if(Content1.equals("中文") && Content2.equals("西班牙语")){
                            from = "zh";
                            to = "spa";
                        }else if(Content1.equals("中文") && Content2.equals("俄语")){
                            from = "zh";
                            to = "ru";
                        }else if(Content1.equals("中文") && Content2.equals("德语")){
                            from = "zh";
                            to = "de";
                        }else if(Content1.equals("法语") && Content2.equals("英语")){
                            from = "fra";
                            to = "en";
                        }else if(Content1.equals("法语") && Content2.equals("中文")){
                            from = "fra";
                            to = "zh";
                        }else if(Content1.equals("法语") && Content2.equals("日语")){
                            from = "fra";
                            to = "jp";
                        }else if(Content1.equals("法语") && Content2.equals("西班牙语")){
                            from = "fra";
                            to = "spa";
                        }else if(Content1.equals("法语") && Content2.equals("俄语")){
                            from = "fra";
                            to = "ru";
                        }else if(Content1.equals("法语") && Content2.equals("德语")){
                            from = "fra";
                            to = "de";
                        }else if(Content1.equals("日文") && Content2.equals("英语")){
                            from = "fp";
                            to = "en";
                        }else if(Content1.equals("日文") && Content2.equals("中文")){
                            from = "fp";
                            to = "zh";
                        }else if(Content1.equals("日文") && Content2.equals("法语")){
                            from = "jp";
                            to = "fra";
                        }else if(Content1.equals("日文") && Content2.equals("西班牙语")){
                            from = "jp";
                            to = "spa";
                        }else if(Content1.equals("日文") && Content2.equals("俄语")){
                            from = "jp";
                            to = "ru";
                        }else if(Content1.equals("日文") && Content2.equals("德语")){
                            from = "jp";
                            to = "de";
                        }else if(Content1.equals("西班牙语") && Content2.equals("英语")){
                            from = "spa";
                            to = "en";
                        }else if(Content1.equals("西班牙语") && Content2.equals("中文")){
                            from = "spa";
                            to = "zh";
                        }else if(Content1.equals("西班牙语") && Content2.equals("法语")){
                            from = "spa";
                            to = "fra";
                        }else if(Content1.equals("西班牙语") && Content2.equals("日文")){
                            from = "spa";
                            to = "jp";
                        }else if(Content1.equals("西班牙语") && Content2.equals("俄语")){
                            from = "spa";
                            to = "ru";
                        }else if(Content1.equals("西班牙语") && Content2.equals("德语")){
                            from = "spa";
                            to = "de";
                        }else if(Content1.equals("俄语") && Content2.equals("英语")){
                            from = "ru";
                            to = "en";
                        }else if(Content1.equals("俄语") && Content2.equals("中文")){
                            from = "ru";
                            to = "zh";
                        }else if(Content1.equals("俄语") && Content2.equals("法语")){
                            from = "ru";
                            to = "fra";
                        }else if(Content1.equals("俄语") && Content2.equals("日文")){
                            from = "ru";
                            to = "jp";
                        }else if(Content1.equals("俄语") && Content2.equals("西班牙语")){
                            from = "ru";
                            to = "spa";
                        }else if(Content1.equals("俄语") && Content2.equals("德语")){
                            from = "ru";
                            to = "de";
                        }else if(Content1.equals("德语") && Content2.equals("英语")){
                            from = "de";
                            to = "en";
                        }else if(Content1.equals("德语") && Content2.equals("中文")){
                            from = "de";
                            to = "zh";
                        }else if(Content1.equals("德语") && Content2.equals("法语")){
                            from = "de";
                            to = "fra";
                        }else if(Content1.equals("德语") && Content2.equals("日文")){
                            from = "de";
                            to = "jp";
                        }else if(Content1.equals("德语") && Content2.equals("西班牙语")){
                            from = "de";
                            to = "spa";
                        }else if(Content1.equals(Content2)){

                            ts.show();
                            return;
                        }
                        else{
                            from = "de";
                            to = "ru";
                        }
                            com.example.everything_is_possible.TransApi api = new com.example.everything_is_possible.TransApi();
                            String transResult = api.getTransResult(text123, from, to);//接口传递
                            if (transResult != null) {//防止空指针出现程序异常退出
                                String parse = ParseResult.parseresult(transResult);
                                handler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        text2.setText(parse);//文本框写入
                                    }
                                });
                            }
                        }


                }).start();
            }
        });
    }


}
